<template>
	<el-container>
		<el-aside width="200px" v-loading="menuloading">
			<el-container>
				<el-header>
					<el-input placeholder="输入关键字进行过滤" v-model="menuFilterText" clearable></el-input>
				</el-header>
				<el-main class="nopadding">
					<el-tree ref="menu" class="menu" node-key="id" :data="menuList" :props="menuProps" :current-node-key="''"
							 draggable highlight-current :expand-on-click-node="false"
							 :filter-node-method="menuFilterNode" @node-click="menuClick"></el-tree>
				</el-main>
				<el-footer style="height:51px;">
					<el-button type="primary" size="small" icon="el-icon-plus" v-auth="'bomType.add'" @click="addType"></el-button>
					<el-button type="success" size="small" plain icon="el-icon-edit" v-auth="'bomType.edit'" @click="editMenu"></el-button>
					<el-button type="danger" size="small" plain icon="el-icon-delete" v-auth="'bomType.delete'" @click="delMenu"></el-button>
				</el-footer>
			</el-container>
		</el-aside>
		<el-container>
			<el-header>
				<div class="right-panel">
					<div class="right-panel-search">
						<el-input v-model="search.keywords" placeholder="BOM单编号/商品编码/商品名称" clearable @change="handleKeyDown"></el-input>
						<el-select v-model="search.pay_type" filterable placeholder="是否禁用">
							<el-option label="是" :value="1"/>
							<el-option label="否" :value="2"/>
						</el-select>
						<el-select v-model="search.pay_type" filterable placeholder="审核状态">
							<el-option label="是" :value="1"/>
							<el-option label="否" :value="2"/>
						</el-select>
						<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
						<el-button type="info" @click="refresh">重置</el-button>
					</div>
				</div>
				<div class="left-panel">
					<el-button type="primary" @click="add" v-auth="'bom.main'">多级维护</el-button>
					<el-button type="primary" @click="add" v-auth="'bom.add'">新增</el-button>
					<el-button type="primary" @click="batch_show" v-auth="'bom.show'" :disabled="selection.length !== 1">查看</el-button>
					<el-button type="primary" :disabled="selection.length !== 1" @click="batch_edit" v-auth="'bom.edit'">修改</el-button>
					<el-button type="danger" :disabled="selection.length == 0" @click="batch_del" v-auth="'bom.delete'">删除</el-button>
					<el-button type="warning" :disabled="selection.length == 0" @click="checkStatus(1)" v-auth="'bom.status'">禁用</el-button>
					<el-button type="warning" :disabled="selection.length == 0" @click="checkStatus(0)" v-auth="'bom.status'">启用</el-button>
					<el-button type="success" :disabled="selection.length == 0" @click="checkExamine" v-auth="'bom.examine'">审核</el-button>
				</div>
			</el-header>
			<el-main class="nopadding">
				<scTable @row-dblclick="dblclick" ref="table" :apiObj="apiObj" row-key="id"  @selection-change="selectionChange" stripe remoteSort remoteFilter border>
					<el-table-column type="selection" width="50" reserve-selection align="center"></el-table-column>
					<el-table-column label="BOM单编号" prop="bom_code" align="center"></el-table-column>
					<el-table-column label="物料编号" prop="goods_code" align="center"></el-table-column>
					<el-table-column label="物料名称" prop="goods_name" align="center"></el-table-column>
					<el-table-column label="规格型号" prop="goods_spec" align="center"></el-table-column>
					<el-table-column label="基本单位" prop="unit_name" align="center"></el-table-column>
					<el-table-column label="版本号" prop="fver" align="center"></el-table-column>
					<el-table-column label="成品率(%)" prop="rate" align="center"></el-table-column>
					<el-table-column label="是否禁用" prop="status" align="center">
                        <template #default="scope">
							<el-switch v-model="scope.row.status" @change="changeSwitch($event, scope.row)" :loading="scope.row.$switch_status" :active-value="1" :inactive-value="0"></el-switch>
						</template>
                    </el-table-column>
					<el-table-column label="审核状态" prop="check_status" align="center">
                        <template #default="{row}">
                            <el-tag :type="row.check_status == 0 ? 'warning' : row.check_status == 1 ? 'success' : 'danger'">{{row.check_status == 0 ? '待审核' : row.check_status == 1 ? '审核通过' : '审核驳回'}}</el-tag>
                        </template>
                    </el-table-column>
				</scTable>
			</el-main>
		</el-container>
	</el-container>
	<details-dialog v-if="dialog.details" ref="detailsDialog" @success="handleSuccess" @closed="dialog.details = false"></details-dialog>
    <bom-cate v-if="dialog.cate" ref="cateDialog" @success="typeHandleSuccess" @closed="dialog.cate = false"></bom-cate>
    <check-status v-if="dialog.check" ref="checkDialog" @success="handleSuccess" @closed="dialog.check = false"></check-status>
</template>

<script>
import BomCate from './bomCate.vue'
import CheckStatus from './checkStatus.vue'
import detailsDialog from './details.vue'
export default {
	name: 'user',
	components: {detailsDialog, BomCate, CheckStatus},
	data() {
		return {
			dialog: {
				details: false,
				cate: false,
				check: false,
			},
			group: [],
			apiObj: this.$API.bom.bom.list,
			selection: [],
			search: {
				keywords: null,
			},
			menuList: [],
			menuProps: {
				label: (data) => {
					return data.cate_name
				}
			},
			menuloading: false,
			menuFilterText: "",
			menuForm: {},
		}
	},
	watch: {
		menuFilterText(val) {
			this.$refs.menu.filter(val);
		}
	},
	mounted() {
		this.getMenu();
	},
	methods: {
		/**查看点击*/
		batch_show() {
			this.dialog.details = true
			this.$nextTick(() => {
				this.$refs.detailsDialog.open('add')
			})
		},
		//加载树数据
		async getMenu() {
			this.menuloading = true;
			var res = await this.$API.bom.bomCate.list.get();
			this.menuloading = false;
			var allNode = {id: '', cate_name: '所有'}
			res.data.unshift(allNode);
			this.menuList = res.data;
			// this.menuClick(allNode)
		},
		//树点击
		menuClick(data) {
			this.menuForm = data
			var params = {
				bom_cate_id: data.id,
			}
			this.$refs.table.reload(params)
		},
		//树过滤
		menuFilterNode(value, data) {
			if (!value) return true;
			return data.cate_name.indexOf(value) !== -1;
		},
		/**删除菜单*/
		async delMenu() {
			if (!this.menuForm.id) {
				this.$message.warning("请选择需要删除的项")
				return false;
			}
			var confirm = await this.$confirm('确认删除已选择的分类吗？', '提示', {
				type: 'warning',
				confirmButtonText: '删除',
				confirmButtonClass: 'el-button--danger'
			}).catch(() => {
			})
			if (confirm != 'confirm') {
				return false
			}
			this.menuloading = true
			var reqData = {
				id: this.menuForm.id
			}
			var res = await this.$API.answers.cate.delete.post(reqData)
			this.menuloading = false
			if (res.code == 1) {
				this.getMenu()
				this.menuForm = {}
			} else {
				this.$message.warning(res.message)
			}
		},
		/**添加分类*/
		addType() {
			this.dialog.cate = true
			this.$nextTick(() => {
				this.$refs.cateDialog.open('add')
			})
		},
		/**编辑分类*/
		editMenu() {
			if (!this.menuForm.id) {
				this.$message.warning("请选择需要编辑的项")
				return false;
			}
			this.dialog.cate = true
			this.$nextTick(() => {
				this.$refs.cateDialog.open('edit').setData(this.menuForm)
			})
		},
		/**分类添加成功事件*/
		cateHandleSuccess() {
			this.getMenu();
		},
		//添加
		add() {
            let form = {}
            form.zhuangtai = 'add'
			this.$router.push({
				path: '/bom/bom/addBom',
				query: form
			});
		},
        batch_edit() {
            let form = this.selection[0]
			form.zhuangtai = 'edit'
			this.$router.push({
				path: '/bom/bom/addBom',
				query: form
			});
		},
		//编辑
		table_edit(row) {
			let form = row
			form.zhuangtai = 'edit'
			this.$router.push({
				path: '/bom/bom/addBom',
				query: form
			});
		},
		/**双击事件*/
		dblclick(row) {
			this.table_show(row)
		},
		//查看
		table_show(row) {
			let form = row
			form.zhuangtai = 'show'
			this.$router.push({
				path: '/bom/bom/addBom',
				query: form
			});
		},
		//表格内开关
		async changeSwitch(val, row) {
			row.$switch_status = true;
			var res = await this.$API.bom.bom.update_status.post({
				id: [row.id],
				status: row.status,
			});
			if (res.code == 1) {
				row.$switch_status = false;
				this.$refs.table.refresh();
				this.$message.success("操作成功");
			} else {
				this.$alert(res.message, "提示", {type: "error"});
			}
		},
        async checkStatus(e){
            var confirm = await this.$confirm(`确认${e==1?'禁用':'启用'}选中的${this.selection.length}项吗？`, '提示', {
				type: 'warning',
				confirmButtonText: '确定',
				confirmButtonClass: e == 1 ? 'el-button--danger' : 'el-button--primary'
			}).catch(() => {
			})
			if (confirm != 'confirm') {
				return false
			}
            var delete_ids = [];
			this.selection.forEach((item) => {
				delete_ids.push(item.id)
			})
            let data = {
                id: delete_ids,
                status:e
            }
			var res = await this.$API.bom.bom.update_status.post(data)
            if (res.code == 1) {
				this.$refs.table.refresh();
				this.$message.success("操作成功");
			} else {
				this.$alert(res.message, "提示", {type: "error"});
			}
        },
        checkExamine(){
            var ids = [];
			this.selection.forEach((item) => {
				ids.push(item.id)
			})
            this.dialog.check = true
			this.$nextTick(() => {
				this.$refs.checkDialog.open('edit').setData(ids)
			})
        },
        async table_del_all(delete_ids) {
			var reqData = {id: delete_ids}
			var res = await this.$API.bom.bom.delete.post(reqData);
			if (res.code == 1) {
				this.$refs.table.refresh()
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		//批量删除
		async batch_del() {
			var delete_ids = [];
			this.selection.forEach((item) => {
				delete_ids.push(item.id)
			})
			this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
				type: 'warning'
			}).then(() => {
				this.table_del_all(delete_ids)
			}).catch(() => {

			})
		},
		//搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
		//本地更新数据
		handleSuccess(data, mode) {
			if (mode == 'add') {
				this.$refs.table.refresh()
			} else if (mode == 'edit') {
				this.$refs.table.refresh()
			}
		},
		selectionChange(selection) {
			this.selection = selection;
		},
        typeHandleSuccess() {
			this.getMenu();
		},
	}
}
</script>

<style>
.search-input {
	width: auto;
	font-size: 14px;
}

.shopInfo {
	display: flex;
	align-items: center;
}

.img {
	width: 100px;
	height: 100px;
	margin-right: 5px;
}
</style>
